package Sort;

public class BubbleSort {
    /**
     * 冒泡排序
     */
    public static void bubbleSort(int[] arr){
        for (int i = 0; i <arr.length-1; i++) {
            boolean flg=false;
            for (int j = 0; j <arr.length-1-i; j++) {
                if(arr[j]>arr[j+1]){
                    swap(arr,j,j+1);
                    flg=true;
                }
            }
            if (flg==false){
                break;
            }
        }
    }
    public static void swap(int[] arr,int i,int j){
        int tem=arr[i];
        arr[i]=arr[j];
        arr[j]=tem;
    }
    public static void main(String[] args) {
        int[] array={9,8,7,6,5,4,3,2,1};
        bubbleSort(array);
        for (int i = 0; i <array.length; i++) {
            System.out.print(array[i]+" ");
        }
    }
}
